<script setup>
import { inject, ref } from "vue";
import { addressListApi } from "@/services/address";
import { onLoad, onShow } from "@dcloudio/uni-app";
import { useaddressStore } from "@/stores/address";

const dateobj = useaddressStore();
const type = ref(null);

const addAddress = () => {
  uni.navigateTo({
    url: "/pages/address_add/index",
  });
};

onLoad((e) => {
  type.value = e.type ? e.type : null;
});
onShow(() => {
  addressListFun();
});

const addressData = ref([]);

async function addressListFun() {
  //发送请求
  let res = await addressListApi({ page: 1 });
  addressData.value = res.data;
}

// 编辑
const updateAdderss = (address_id) => {
  uni.navigateTo({
    url: `/pages/address_add/index?address_id=${address_id}`,
  });
};

const selectaddress = (item) => {
  // goPageA(JSON.stringify(item));

  console.log(item, type.value);

  if (type.value == "add") {
    dateobj.setaddress(JSON.stringify(item));
    // // 4. 返回上一页面
    uni.navigateBack({
      delta: 1, // 返回的页面数
    });
  }
  if (type.value == "addup") {
    dateobj.setaddress(JSON.stringify(item));
    // // 4. 返回上一页面
    uni.navigateBack({
      delta: 1, // 返回的页面数
    });
  }
};
</script>

<template>
  <view class="content">
    <view
      class="content-list"
      v-show="addressData && addressData.list && addressData.list.length > 0"
    >
      <view
        class="content-list-item"
        v-for="(item, index) in addressData && addressData.list"
        :key="index"
        @tap.stop="selectaddress(item)"
      >
        <view class="content-list-title">
          <view class="content-list-name">
            <view class="content-list-nameorphone">
              <text>{{ item.real_name }}</text>
              <text>{{ item.phone }}</text>
            </view>
            <text class="select-item" v-if="item.is_default">默认</text>
          </view>
          <view class="content-list-address">
            {{ item.province + item.city + item.district + item.detail }}
          </view>
        </view>
        <view class="content-icon" @tap.stop="updateAdderss(item.address_id)">
          <up-icon name="edit-pen" color="#8C8C8C" size="48rpx"></up-icon>
        </view>
      </view>
    </view>

    <view
      class="nodate-bt"
      v-if="addressData && addressData.list && addressData.list.length == 0"
    >
      <image src="/static/nodate/nodate.png" mode="widthFix"></image>
    </view>

    <view class="foodr-bt">
      <view class="foodr-bt-k">
        <view class="bt-sbuit" @click.shop="addAddress">
          <up-icon name="plus" color="#FFFFFF" size="28rpx"></up-icon>
          <text class="add-text">新增地址</text>
        </view>
      </view>
    </view>
  </view>
</template>

<style lang="scss" scoped>
@import "./index.scss";
</style>
